<template>
  <div>
    <p class="pinformation">搜索条件</p>
    <el-form label-width="80px" ref="queryRef" :model="queryParams">
      <el-row>
        <el-col :span="4">
          <el-form-item label="采样人员：" prop="cyUserName">
            <el-input maxlength="100" clearable placeholder="请输入采样人员" v-model="queryParams.cyUserName"
              @keyup.enter="handleQuery" :disabled="!props.projectId" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="采样时间：" prop="cyDate">
            <el-date-picker v-model="queryParams.cyDate" placeholder="采样时间" type="date" value-format="YYYY-MM-DD"
              :disabled="!props.projectId" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="收样人：" prop="getUserName">
            <el-input maxlength="100" clearable placeholder="请输入收样人" v-model="queryParams.getUserName"
              @keyup.enter="handleQuery" :disabled="!props.projectId" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="收样时间：" prop="getDate">
            <el-date-picker v-model="queryParams.getDate" placeholder="收样时间" type="date" value-format="YYYY-MM-DD"
              :disabled="!props.projectId" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="状态：" prop="status" v-if="queryParams.userStatus == 1">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery"
              :disabled="!props.projectId">
              <el-option v-for="dict in proxy.addListNoLimit(sys_sample_with_status)" :key="dict.value"
                :label="dict.label" :value="parseInt(dict.value)" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-row justify="center">
            <el-button type="success" @click="handleQuery" :disabled="!props.projectId">搜索</el-button>
            <el-button class="business resetBtn" @click="resetQuery" :disabled="!props.projectId">重置</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-row>
        <p class="pinformation">样品交接表列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
      </el-row>
      <el-row style="padding-bottom: 5px">
        <el-button type="success" style="margin-left: 10px" link @click="AddOpenSampleWith"
          :disabled="!props.projectId">新增</el-button>
        <el-button type="success" link :disabled="!props.projectId">源自储运表</el-button>
      </el-row>
    </el-row>
    <el-table :data="sampleWithList" ref="sampleWithListTableRef" v-loading="loading">
      <el-table-column label="批次序号" align="center" prop="withNumber" />
      <el-table-column label="采样人员" align="center" prop="cyUserName" />
      <el-table-column label="采样时间" align="center">
        <template #default="scope">
          {{ scope.row.cyStartDate }}
          {{ scope.row.cyStartDate || scope.row.cyEndDate ? "-" : "" }}
          {{ scope.row.cyEndDate }}
        </template>
      </el-table-column>
      <el-table-column label="收样人" align="center" prop="getUserName" />
      <el-table-column label="收样时间" align="center" prop="getDate" />
      <el-table-column label="样品数量" align="center" prop="sampleCount" width="100" />
      <el-table-column label="状态" align="center" width="130">
        <template #default="scope">
          <dict-tag :options="sys_sample_with_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-button link type="success" @click="handleCreateWithTable(scope.row)">打开样品交接表</el-button>
          <el-button link type="success">签字</el-button>
          <el-button link type="success" @click="editSampleWith(scope.row)">编辑</el-button>
          <el-button link type="success">查看</el-button>
          <el-button link type="success" @click="handledel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
    <!-- 用户导入对话框 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="open" v-if="open" :title="title" width="1800" @close="closeDialog">
      <addSampleWith :projectId="props.projectId" :withId="withId"></addSampleWith>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="closeDialog">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SampleWithView">
import {
  listProjctView,
  delSampleWith,
  createWithTable,
} from "@/api/sampleManage/sampleWith";
import addSampleWith from "./addSampleWith.vue";
const router = useRouter();
const { proxy } = getCurrentInstance();
const open = ref(false);
const title = ref("");

const { sys_sample_with_status } = proxy.useDict("sys_sample_with_status");

const loading = ref(false);
const total = ref(0);
const sampleWithList = ref([]);

const props = defineProps({
  projectId: {
    type: String, //参数类型
  },
});
const data = reactive({
  form: {
    projectPcList: [],
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const { form, queryParams } = toRefs(data);

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "sampleWithList", bottomHeight: 74 }];
    } else {
      heightParams.value = [{ tableName: "sampleWithList", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "sampleWithList", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

// 记得暴露
defineExpose({
  getList,
  queryParams
});

function getList() {
  queryParams.value.projectId = props.projectId;
  loading.value = true;
  listProjctView(queryParams.value).then((res) => {
    sampleWithList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function AddOpenSampleWith() {
  withId.value = "";
  open.value = true;
  title.value = "新增样品交接";
}

const withId = ref("");

function editSampleWith(row) {
  withId.value = row.withId;
  open.value = true;
  title.value = "编辑样品交接";
}

function handleCreateWithTable(row) {
  withId.value = row.withId;
  router.push({
    path: "/sampleManage/withTable",
    query: { myId: row.myId },
  });
  // proxy.$modal.confirm("确认要生成样品交接表吗？").then(() => {
  //   createWithTable(row.withId).then((res) => {
  //     proxy.$modal.msgSuccess("创建成功");
  //     // 123
  //     let myId = res.data;
  //     //let myId = "1683420839448498177";
  //     router.push({
  //       path: "/sampleManage/withTable",
  //       query: { myId: myId },
  //     });
  //     getList();
  //   });
  // });
}

function closeDialog() {
  open.value = false;
  getList();
}

function handledel(row) {
  proxy.$modal.confirm("确认要删除吗？").then(() => {
    delSampleWith(row.withId).then((res) => {
      proxy.$modal.msgSuccess("删除成功");
      getList();
    });
  });
}
</script>

<style></style>
